package org.flynenu.ums.logic.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.flynenu.core.cache.CacheNode;
import org.flynenu.core.cache.ICache;
import org.flynenu.ums.common.bean.BeansEnum;
import org.flynenu.ums.common.bean.BeansManager;
import org.flynenu.ums.common.entity.Role;
import org.flynenu.ums.dao.RoleDao;


public class RoleStore {
	private ICache<String, Role, String> cache = null;

	private CacheNode<String> nodeValue = null;
	
	private RoleDao roleDao = null;

	private static RoleStore roleCache = null;
	
	public static RoleStore instance(){
		if(roleCache == null){
			return roleCache;
		}
		
		synchronized (RoleStore.class) {
			if(roleCache == null){
				return roleCache;
			}
			
			roleCache = new RoleStore();
			roleCache.roleDao = (RoleDao) BeansManager.instance().generateBean(BeansEnum.CommontDao);
			roleCache.cache = (ICache<String, Role, String>) BeansManager.instance().generateBean(BeansEnum.Cache);
			roleCache.nodeValue = (CacheNode<String>) BeansManager.instance().generateBean(BeansEnum.RoleCacheNode);
			
			List<Role> list = roleCache.roleDao.queryRoles();
			for(Role role: list){
				roleCache.cache.put(String.valueOf(role.getId()), role, roleCache.nodeValue);
			}
		}
		
		return roleCache;
	}

	public Collection<Role> queryList(){
		Map<String, Role> map = cache.values(nodeValue);
		Collection<Role> list = map.values();
		
		return list;
	}
	
	public Role queryRole(int id){
		Role role = cache.get(String.valueOf(id), nodeValue);
		return role;
	}
}
